package cn.edu.ctgu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class PhoneFee {
    private static final SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public long countTime(String date1, String date2) throws ParseException {
        Date fromDate = simpleFormat.parse(date1);
        Date toDate = simpleFormat.parse(date2);
        Calendar fromCal = Calendar.getInstance();
        fromCal.setTime(fromDate);
        Calendar toCal = Calendar.getInstance();
        toCal.setTime(toDate);
        long phoneTime = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
        long minutes = phoneTime / 60000; // Convert milliseconds to minutes
        long seconds = (phoneTime % 60000) / 1000; // Remaining milliseconds converted to seconds
        if (seconds > 0) {
            minutes++; // Round up to the nearest minute if there are any remaining seconds
        }

        // Adjust for DST (Daylight Saving Time)
        Calendar marchLastSunday = Calendar.getInstance();
        marchLastSunday.set(Calendar.MONTH, Calendar.MARCH);
        marchLastSunday.set(Calendar.WEEK_OF_MONTH, marchLastSunday.getActualMaximum(Calendar.WEEK_OF_MONTH));
        marchLastSunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        marchLastSunday.set(Calendar.HOUR_OF_DAY, 2);
        marchLastSunday.set(Calendar.MINUTE, 0);
        marchLastSunday.set(Calendar.SECOND, 0);
        marchLastSunday.set(Calendar.MILLISECOND, 0);

        Calendar novemberFirstSunday = Calendar.getInstance();
        novemberFirstSunday.set(Calendar.MONTH, Calendar.NOVEMBER);
        novemberFirstSunday.set(Calendar.WEEK_OF_MONTH, 1);
        novemberFirstSunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        novemberFirstSunday.set(Calendar.HOUR_OF_DAY, 2);
        novemberFirstSunday.set(Calendar.MINUTE, 59);
        novemberFirstSunday.set(Calendar.SECOND, 59);
        novemberFirstSunday.set(Calendar.MILLISECOND, 999);

        if ((fromCal.after(marchLastSunday) && fromCal.before(novemberFirstSunday)) ||
                (toCal.after(marchLastSunday) && toCal.before(novemberFirstSunday))) {
            // Adjust for DST if either time falls within DST period
            if ((fromCal.get(Calendar.MONTH) == Calendar.MARCH && fromCal.get(Calendar.WEEK_OF_MONTH) == marchLastSunday.get(Calendar.WEEK_OF_MONTH) &&
                    fromCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY && fromCal.get(Calendar.HOUR_OF_DAY) == 2 &&
                    toCal.get(Calendar.HOUR_OF_DAY) >= 3) ||
                    (toCal.get(Calendar.MONTH) == Calendar.NOVEMBER && toCal.get(Calendar.WEEK_OF_MONTH) == 1 &&
                            toCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY && toCal.get(Calendar.HOUR_OF_DAY) == 2 &&
                            fromCal.get(Calendar.HOUR_OF_DAY) < 3)) {
                // Adjust for DST transition
                if (fromCal.before(marchLastSunday) && toCal.after(marchLastSunday)) {
                    minutes += 60; // Spring forward: 2 AM to 3 AM becomes 1 hour longer
                } else if (fromCal.after(novemberFirstSunday) && toCal.before(novemberFirstSunday)) {
                    minutes -= 60; // Fall back: 2:59:59 AM to 2:00:00 AM becomes 1 hour shorter
                }
            }
        }
        return minutes;
    }

    public double countMoney(long time) {
        if (time <= 0 || time > 1800) { // 1800 minutes = 30 hours
            return -1.0; // Error case
        } else if (time <= 20) {
            return time * 0.05;
        } else {
            return 1 + (time - 20) * 0.1;
        }
    }

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.print("Enter start time (yyyy-MM-dd HH:mm:ss): ");
            String fromTime = br.readLine();
            System.out.print("Enter end time (yyyy-MM-dd HH:mm:ss): ");
            String toTime = br.readLine();
            PhoneFee phoneFee = new PhoneFee();
            long minutes = phoneFee.countTime(fromTime, toTime);
            double money = phoneFee.countMoney(minutes);
            System.out.println("Charge: $" + money);
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }
}